package engine.newUi
{
	import engine.control.NxTablePageTurner;
	import engine.data.ModelLocator;
	import engine.logic.MenuListManager;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.com.config.StaticIni;
	import newx.com.control.table.NxTableEvent;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *显示帮派所有成员界面逻辑 
	 * @author zjing
	 * 
	 */
	public class FamilyBodyLogic
	{
		private var _ui : FamilyBodyUI;
		public static var _self : FamilyBodyLogic;
		
		private var _model : ModelLocator;
		private var _pageManager : NxTablePageTurner;
		
		public function FamilyBodyLogic()
		{
		}
		
		public function Init():void
		{
			_model = ModelLocator.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_OTHER_GUILD_INFO),onFamilyBodyReturn);
			_pageManager = new NxTablePageTurner(ui.btnPre,ui.btnNext,ui.listTab,ui.labPage);
		}
		
		
		
		public function BeforeShow():void
		{
			ui.windowBG	.addEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
			ui.listTab .addEventListener(	NxTableEvent.TABLE_CLICK,	onClickTableHandle);
			_pageManager.addEventListener();
		}
		
		public function AfterHide():void
		{
			ui.windowBG	.removeEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
			ui.listTab .addEventListener(	NxTableEvent.TABLE_CLICK,	onClickTableHandle);
			_pageManager.addEventListener();
		}
		
		/**
		 *查询帮派所有成员信息接收 
		 * 数据列数(职务，名称，等级，职业，城堡，状态(0离线 1在线), 在线时间, 今天日贡献, 总贡献, 排行榜排名。)
		 * @param e
		 * 
		 */
		private function onFamilyBodyReturn(e:CustomMsgEvent):void
		{
			var p:int;
			ui.familyName.htmlText = e.pList[p++];
			ui.familyBigMember.htmlText = e.pList[p++];
			ui.familyLevel.htmlText = e.pList[p++];
			var familyExp : String = e.pList[p++];
			ui.familyJinYan.htmlText = familyExp+"/"+StaticIni.getIniVar("familyUP_"+ui.familyLevel.text,"exp");
			var listLength : int = e.pList[p++];
			ui.familyMemberNum.htmlText = listLength.toString();
			ui.familyPaiMing.htmlText = e.pList[p++];
			
			var FamilyMemberes : Array = new Array;
			var dataPrivater : Array = new Array;
			for(var i:int=0;i<listLength;i++)
			{
				var memberObj : Object = new Object();
				memberObj["familyPosition"] = getFamilyPosition(e.pList[p++]);
				memberObj["memberName"] = e.pList[p++];
				memberObj["memberLevel"] = e.pList[p++];
				memberObj["memberSchool"] = getFamilySchool(e.pList[p++]);//门派
				memberObj["memberChengbao"] = e.pList[p++];
				var online : int = e.pList[p++];
				memberObj["memberIsOnline"] = online;
				memberObj["memberOnlineTime"] = e.pList[p++];
				memberObj["memberTodayContribute"] = e.pList[p++];
				memberObj["memberAllContribute"] = e.pList[p++];
				var memberArenaPosition : int = e.pList[p++];
				memberObj["memberArenaPosition"] = memberArenaPosition == 0?StaticIni.getSwfLangStr("Str_NotInTopList"):memberArenaPosition;
				dataPrivater.push([{label:setHtmlText(online,memberObj["familyPosition"])},
					{label:setHtmlText(online,memberObj["memberName"])},
					{label:setHtmlText(online,memberObj["memberLevel"])},
					{label:setHtmlText(online,memberObj["memberSchool"])},
					{label:setHtmlText(online,memberObj["memberArenaPosition"])}]);
				FamilyMemberes.push(memberObj);
			}
			ui.listTab.dataProvider = dataPrivater;
			_pageManager.onPageDis();
			ui.show();
		}
		
		/**
		 *打开加基友，私聊，查看menuList 
		 * @param e
		 * 
		 */
		private function onClickTableHandle(e : NxTableEvent):void
		{
			if(e.lineArrData!=null)
			{
				var onClickMemberName : String = e.lineArrData[1].label;
				if(onClickMemberName == "") return;
				var textField:TextField = new TextField();
				textField.htmlText = onClickMemberName;
				var playName:String = textField.text;
				if(playName != _model.scene.Role.getPlayerName())
				{
					MenuListManager.clickPlayer(playName);
				}
			}
		}
		
		/**
		 *是否在线添加Html标签 
		 * @param onLine
		 * @param str
		 * @return 
		 * 
		 */
		private function setHtmlText(onLine:int,str:String):String
		{
			return onLine == 1 ? "<font color ='#1eff00'>"+str+"</font>" : "<font color='#9d9d9d'>"+str+"</font>";
		}
		
		/**
		 *获取帮派职务 
		 * @param position（1帮主2副帮主3帮众）
		 * @return 
		 * 
		 */
		private function getFamilyPosition(position:int):String
		{
			return StaticIni.getSwfLangStr("FamilyPosition_"+position.toString());
		}
		
		/**
		 *获取帮派成员门派名称 
		 * @param school
		 * @return 
		 * 
		 */
		private function getFamilySchool(school:int):String
		{
			return StaticIni.getSwfLangStr("NationString_00"+school.toString());
		}
		
		private function onClickCloseHandle(e : NxWindowEvent):void
		{
			ui.showOrHide();
		}
		
		public function get ui():FamilyBodyUI
		{
			if(!_ui)
			{
				_ui = new FamilyBodyUI();
			}
			return _ui;
		}
		
		public static function getInstance():FamilyBodyLogic
		{
			if(!_self)
			{
				_self = new FamilyBodyLogic();
			}
			return _self;
		}
	}
}